// pages/index1/index1.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tableData: [
      { id: 1, item: '', selected: false, status: false, buttonActive: false },
      { id: 2, item: '', selected: false, status: false, buttonActive: false },
      { id: 3, item: '', selected: false, status: false, buttonActive: false },
    ],
    headerData: [
      { label: '序号' },
      { label: '物品名称' },
      { label: '状态' },
    ],
    isButtonActive: false,
    longPressTimer: null,
    isEditMode: true,
    isPageEditable: true,
    itemInputCache: [],
    webUrl: "https://i.mi.com/mobile/find#/",
  },

  addRow: function () {
    var tableData = this.data.tableData
    tableData.push({ id: '', item: '', selected: false, status: false })
    this.setData({
      tableData: tableData,
    })
  },

  deleteRow: function () {
    var tableData = this.data.tableData
    if (tableData.length > 0) {
      tableData.pop()
      this.setData({
        tableData: tableData,
      })
    }
  },

  /*
  onItemLongPressStart: function (event) {
    var index = event.currentTarget.dataset.index
    var tableData = this.data.tableData
    var selected = tableData[index].selected
    if (selected) {
      tableData[index].selected = false
    } else {
      tableData[index].selected = true
    }
    this.setData({
      tableData: tableData,
    })
    this.data.longPressTimer = setTimeout(() => {
      this.data.longPressTimer = null
    }, 1000) // 长按持续时间，单位为毫秒
  },

  onItemLongPressEnd: function (event) {
    if (this.data.longPressTimer) {
      clearTimeout(this.data.longPressTimer)
      this.data.longPressTimer = null
    }
  },
  */
 
  onItemInput: function (event) {
    var index = event.currentTarget.dataset.index
    var field = event.currentTarget.dataset.field
    var value = event.detail.value
    //var tableData = this.data.tableData
    var itemInputCache = this.data.itemInputCache; // 获取临时变量
    if (!itemInputCache[index]) {
      itemInputCache[index] = {}; // 初始化缓存
    }
    itemInputCache[index][field] = value;
    this.setData({
      itemInputCache: itemInputCache, // 更新临时变量
    });
  },

  saveItemInputs: function () {
    var itemInputCache = this.data.itemInputCache;
    var tableData = this.data.tableData;
    itemInputCache.forEach((item, index) => {
      tableData[index] = tableData[index] || {}; // 确保 tableData[index] 已定义
      tableData[index].item = item.item;
    });
    this.setData({
      tableData: tableData,
      itemInputCache: [],
    });
  },
  
  resetSelectedStatus: function (index) {
    var tableData = this.data.tableData
    tableData[index].selected = false
    this.setData({
      tableData: tableData,
    })
  },

  toggleStatus: function (event) {
    var index = event.currentTarget.dataset.index;
    var tableData = this.data.tableData;
    var status = tableData[index].status;
    tableData[index].status = !tableData[index].status;
    this.setData({
      tableData: tableData,
    });
  },

  onItemLongPressStart: function (event) {
    var index = event.currentTarget.dataset.index;
    var tableData = this.data.tableData;
    var selected = tableData[index].selected;
    if (selected) {
      tableData[index].selected = false;
    } else {
      tableData[index].selected = true;
    }
    this.setData({
      tableData: tableData,
    });
    this.data.longPressTimer = setTimeout(() => {
      this.data.longPressTimer = null;
      this.toggleStatusIcon(index); // 在长按结束时切换状态
    }, 1000); // 长按持续时间，单位为毫秒
  },

  onItemLongPressEnd: function (event) {
    if (this.data.longPressTimer) {
      clearTimeout(this.data.longPressTimer);
      this.data.longPressTimer = null;
    }
  },

  toggleStatusIcon: function (index) {
    var tableData = this.data.tableData;
    tableData[index].status = !tableData[index].status;
    this.setData({
      tableData: tableData,
    });
  },
  
  toggleEditMode: function () {
    var isEditMode = !this.data.isEditMode;
    var isPageEditable = isEditMode;
    this.setData({
      isEditMode: isEditMode,
      isPageEditable: isPageEditable,
    });
  },

  clearAllContent: function () {
    this.setData({
      tableData: [{ id: '', item: '', status: false }],
    });
  },

  navigateToWebPage: function() {
    wx.navigateTo({
      url: "/pages/webview/webview?url=" + encodeURIComponent(this.data.webUrl)
    });
  },

  toggleButtonColor: function(event) {
    var index = event.currentTarget.dataset.index;
    var tableData = this.data.tableData;
    tableData[index].buttonActive = !tableData[index].buttonActive;
    this.setData({
      tableData: tableData,
    });
  },
  
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})